package com.zwps.common.controller.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.util.ContentCachingRequestWrapper;

/**
 * 缓存请求内容
 * @ClassName: ContentCachingRequestFilter
 * @Description: 
 * @author lyh
 * @date 2023年10月20日 下午3:45:00
 *
 */
public class ContentCachingRequestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            if (request.getContentType() != null
                    && !request.getContentType().contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
                chain.doFilter(new ContentCachingRequestWrapper((HttpServletRequest) request), response);
                return;
            }
        }
        chain.doFilter(request, response);
        return;
    }
}
